package logic.ships.moveableShip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;

import logic.slots.AbstractSlotCreator;

public class NozzleSlotCreator extends AbstractSlotCreator {
	
	private final String NOZZ = "nozz";
	private final String INNER = "inner";
	private final String OUTER = "outer";
	
	private List<NozzleSlot> nozzleSlots;
	private HashMap<String, TriMesh> inners, outers;
	
	private MovableShip ship;
	
	public NozzleSlotCreator(MovableShip ship) {
		this.ship = ship;
		
		inners = new HashMap<String, TriMesh>();
		outers = new HashMap<String, TriMesh>();
		
		nozzleSlots = new ArrayList<NozzleSlot>();
		
		init(ship.getLowQualityNode());
		createSlots();
	}
	
	private void init(Node parent) {
		for(Spatial child : parent.getChildren()) {
			if(child instanceof Node) init((Node)child);
			else if(child instanceof TriMesh) {
				TriMesh mesh = (TriMesh)child;
				String name = mesh.getName();
				
				String[] array = name.split("_");
				if(array.length != 2) continue;
				
				name = array[0].substring(0, array[0].length() - 1);
				if(!name.equals(NOZZ)) continue;
				
				if(array[1].equals(INNER)) inners.put(array[0], mesh);
				else {
					assert(array[1].equals(OUTER));
					outers.put(array[0], mesh);
				}
			}
		}
	}
	
	private void createSlots() {		
		for(String name : inners.keySet()) {
			TriMesh inner = inners.get(name);
			TriMesh outer = outers.get(name);
			
			assert(inner != null && outer != null);
			
			NozzleSlot slot = new NozzleSlot(inner, outer, ship);
			ship.attachChild(slot);
			
			nozzleSlots.add(slot);
		}
	}
		
	public List<NozzleSlot> getNozzleSlots() { return nozzleSlots; }
}